import 'package:bi_project/model/home_mo.dart';
import 'package:bi_project/navigator/hi_navigator.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper_view/flutter_swiper_view.dart';

class HiBanner extends StatelessWidget {
  final List<BannerListMo> bannerList;
  final double? bannerHeight;
  final EdgeInsetsGeometry? padding;
  const HiBanner(this.bannerList,
      {super.key, this.bannerHeight = 160, this.padding});

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: bannerHeight,
      child: _banner(),
    );
  }

  _banner() {
    var right = 10 + (padding?.horizontal ?? 0) / 2;
    return Swiper(
      autoplay: true,
      itemCount: bannerList.length,
      itemBuilder: (context, index) {
        return _image(bannerList[index]);
      },
      pagination: SwiperPagination(
          alignment: Alignment.bottomRight,
          margin: EdgeInsets.only(right: right, bottom: 10),
          builder: const DotSwiperPaginationBuilder(
              color: Colors.white60, size: 6, activeSize: 12)),
    );
  }

  _image(BannerListMo bannerListItem) {
    return InkWell(
      onTap: () {
        print(bannerListItem.title);
        _handleClick(bannerListItem);
      },
      child: ClipRRect(
        borderRadius: const BorderRadius.all(Radius.circular(6)),
        child: Image.network(
          bannerListItem.cover,
          fit: BoxFit.cover,
        ),
      ),
    );
  }

  void _handleClick(BannerListMo bannerListItem) {
    if (bannerListItem.type == "video") {
      HiNavigator.getInstance().onJumpTo(RouteStatus.detail,
          args: {"videoMo": VideoListMo(vid: bannerListItem.sticky)});
    } else {
      print(bannerListItem.url);
      // todo
    }
  }
}
